#include "gamewindow.h"
#include "ui_gamewindow.h"

const int GRID_SIZE = 20;
const int GAME_WIDTH = 30;
const int GAME_HEIGHT = 25;

GameWindow::GameWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::GameWindow)
{
    ui->setupUi(this);
    setFixedSize(GAME_WIDTH * GRID_SIZE, GAME_HEIGHT * GRID_SIZE);
    
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &GameWindow::updateGame);
    timer->start(150);
    
    // 初始化蛇
    snake.append(QPoint(GAME_WIDTH / 2, GAME_HEIGHT / 2));
    snake.append(QPoint(GAME_WIDTH / 2 - 1, GAME_HEIGHT / 2));
    snake.append(QPoint(GAME_WIDTH / 2 - 2, GAME_HEIGHT / 2));
    
    direction = 1; // 初始向右
    gameOver = false;
    score = 0;
    
    generateFood();
}

GameWindow::~GameWindow()
{
    delete ui;
}

void GameWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    
    QPainter painter(this);
    
    // 绘制背景
    painter.fillRect(0, 0, width(), height(), Qt::black);
    
    // 绘制蛇
    painter.setBrush(Qt::green);
    for (const auto& segment : snake) {
        painter.drawRect(segment.x() * GRID_SIZE, segment.y() * GRID_SIZE, 
                         GRID_SIZE - 1, GRID_SIZE - 1);
    }
    
    // 绘制食物
    painter.setBrush(Qt::red);
    painter.drawEllipse(food.x() * GRID_SIZE, food.y() * GRID_SIZE, 
                       GRID_SIZE, GRID_SIZE);
    
    // 游戏结束文字
    if (gameOver) {
        painter.setPen(Qt::white);
        painter.setFont(QFont("Arial", 20));
        painter.drawText(rect(), Qt::AlignCenter, 
                        QString("游戏结束！得分: %1\n按空格键重新开始").arg(score));
    }
    
    // 绘制分数
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 12));
    painter.drawText(10, 20, QString("分数: %1").arg(score));
}

void GameWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Up:
        if (direction != 2) direction = 0;
        break;
    case Qt::Key_Right:
        if (direction != 3) direction = 1;
        break;
    case Qt::Key_Down:
        if (direction != 0) direction = 2;
        break;
    case Qt::Key_Left:
        if (direction != 1) direction = 3;
        break;
    case Qt::Key_Space:
        if (gameOver) {
            // 重置游戏
            snake.clear();
            snake.append(QPoint(GAME_WIDTH / 2, GAME_HEIGHT / 2));
            snake.append(QPoint(GAME_WIDTH / 2 - 1, GAME_HEIGHT / 2));
            snake.append(QPoint(GAME_WIDTH / 2 - 2, GAME_HEIGHT / 2));
            direction = 1;
            gameOver = false;
            score = 0;
            generateFood();
        }
        break;
    }
}

void GameWindow::updateGame()
{
    if (gameOver) return;
    
    // 获取蛇头位置
    QPoint head = snake.first();
    
    // 根据方向移动蛇头
    switch (direction) {
    case 0: // 上
        head.setY(head.y() - 1);
        break;
    case 1: // 右
        head.setX(head.x() + 1);
        break;
    case 2: // 下
        head.setY(head.y() + 1);
        break;
    case 3: // 左
        head.setX(head.x() - 1);
        break;
    }
    
    // 检查是否碰撞边界
    if (head.x() < 0 || head.x() >= GAME_WIDTH || 
        head.y() < 0 || head.y() >= GAME_HEIGHT) {
        gameOver = true;
        update();
        return;
    }
    
    // 检查是否碰撞自身
    if (snake.contains(head)) {
        gameOver = true;
        update();
        return;
    }
    
    // 将新头添加到蛇身
    snake.prepend(head);
    
    // 检查是否吃到食物
    if (head == food) {
        score += 10;
        generateFood();
    } else {
        // 没吃到食物则移除尾部
        snake.removeLast();
    }
    
    update();
}

void GameWindow::generateFood()
{
    // 生成不在蛇身上的食物位置
    do {
        food.setX(QRandomGenerator::global()->bounded(GAME_WIDTH));
        food.setY(QRandomGenerator::global()->bounded(GAME_HEIGHT));
    } while (snake.contains(food));
}    